﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Interop;
using System.Windows;

namespace KZ400B.Controls
{
    public class IconManage
    {
        private IconManage(
            Bitmap r2Zero
            ,Bitmap rZeroSet
            ,Bitmap rRun
            ,Bitmap rStop
            ,Bitmap pExecute
            ,Bitmap vZero
            )
        {
            R2Zero = ChangeBitmapToBitmapSource(r2Zero);
            RZeroSet = ChangeBitmapToBitmapSource( rZeroSet );
            RRun = ChangeBitmapToBitmapSource(rRun);
            RStop = ChangeBitmapToBitmapSource(rStop);
            PExecute3 = ChangeBitmapToBitmapSource(pExecute);
            VZero = ChangeBitmapToBitmapSource(vZero);
        }
        private static IconManage m_Instance;
        public static IconManage getInstance(
            Bitmap r2Zero
            , Bitmap rZeroSet
            , Bitmap rRun
            , Bitmap rStop
            , Bitmap pExecute
            , Bitmap vZero
            )
        {
            if (m_Instance == null)
            {
                m_Instance = new IconManage(r2Zero,rZeroSet,rRun,rStop,pExecute,vZero);
            }
            return m_Instance;
        }
        public ImageSource R2Zero { get; private set; }
        public ImageSource RZeroSet { get; private set; }
        public ImageSource RRun { get; private set; }
        public ImageSource RStop { get; private set; }
        public ImageSource PExecute3 { get; private set; }
        public ImageSource VZero { get; private set; }

        /// <summary>
        /// 从Bitmap转换成BitmapSource
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static BitmapSource ChangeBitmapToBitmapSource( Bitmap bmp)
        {

            BitmapSource returnSource;
            try
            {
               returnSource = Imaging.CreateBitmapSourceFromHBitmap
                   (
                        bmp.GetHbitmap(), IntPtr.Zero, 
                        Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()
                   );
            }
            catch
            {
                returnSource = null;
            }
            return returnSource;
        }

    }
}
